package com.company.jdk8;

import com.company.MyInterface;
import org.junit.Test;

import javax.sound.midi.Soundbank;
import java.net.SocketTimeoutException;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.logging.SocketHandler;
import java.util.stream.Stream;

/**
 * @ProjectName: 复习java
 * @Package: com.company.jdk8
 * @ClassName: StreamApiTest1
 * @Author: wym
 * @Date: 2020/11/9 20:45
 * @Version: 1.0
 * Stream 中间操作
 */
public class StreamApiTest1 {
        @Test
    public void test(){
            List<Employee> employees = EmployeeData.getEmployees();
            Stream<Employee> stream = employees.stream();
            //e 就是当前的对象 filter
           // stream.filter(e->e.getSalary()>7000).forEach(e-> System.out.println(e));
            //limit(截断流)  一旦操作了流 终止操作接下来的就没用了
            stream.limit(10).forEach(i-> System.out.println(i));
            //skip 跳过
            stream.skip(3).forEach(i-> System.out.println(i));
            //distinct() 去重复数据 根据hashcode  equals() 方法来除掉重复元素
            stream.distinct().forEach(i-> System.out.println(i));
        }
        @Test
    public void test2(){
//            List<Employee> employees = EmployeeData.getEmployees();
//            employees.stream().map()
        //map 映射
//            List<String> list = Arrays.asList("aa", "bb", "cc");
//            list.stream().map(e->e.toUpperCase()).forEach(i-> System.out.println(i));
            //获取员工姓名长度大于3的员工的姓名
            List<Employee> employees = EmployeeData.getEmployees();
            employees.stream()
                    .filter(e->e.getName().length()>3)
                    .map(e->e.getName())
                    .forEach(i-> System.out.println(i));
        }
       @Test
    public void test3(){
           List<Integer> list = Arrays.asList(1, 3, 4, 5, 2, 45, 53);
           //自然排序
           list.stream().sorted().forEach(System.out::println);
            //定制排序
           List<Employee> employees = EmployeeData.getEmployees();
           Stream<Employee> stream = employees.stream();
           stream.sorted((e1,e2)->{
               int compareValue = Integer.compare(e1.getAge(), e2.getAge());
               if (compareValue!=0){
                   return compareValue;
               }else {//继续根据工资进行排序
                   return -Double.compare(e1.getSalary(),e2.getSalary());
               }
           }).forEach(System.out::println);
       }

       @Test
        public  void  test4(){
            //练习 是否存在员工的工资大于10000的
           List<Employee> employees = EmployeeData.getEmployees();
           Stream<Employee> stream = employees.stream();
           //anyMatch
           boolean b = stream.anyMatch(e -> e.getSalary() > 10000);
           System.out.println(b);
            //是否存在雷性的
           boolean 雷 = stream.noneMatch(e -> e.getName().startsWith("雷"));
           //findFirst 返回第一个

           //返回当前流中的任意元素 findAny()

       }

       @Test
       public void  test5(){
          //count
           List<Employee> employees = EmployeeData.getEmployees();
           long count = employees.stream().filter(e -> e.getSalary() > 5000).count();
           //max
           Optional<Employee> max = employees.stream().max((e1, e2) -> {
               return Double.compare(e1.getSalary(), e2.getSalary());
           });
           System.out.println(max);
           //min
           Optional<Employee> min = employees.stream().min((e1, e2) -> {
               return Double.compare(e1.getSalary(), e2.getSalary());
           });
           System.out.println(min);
       }
}
